package com.shadowtorcher.TableRolePlayingSystem.util;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Set;

public abstract class BiMap<K, V> extends AbstractMap<K, V> {
    private final HashMap<K, V> kv = new HashMap<>();
    private final HashMap<V, K> vk = new HashMap<>();

    @Override
    public Set<Entry<K, V>> entrySet() {
        return kv.entrySet();
    }

    public Set<K> keySet() {
        return kv.keySet();
    }

    public Set<V> valueSet() {
        return vk.keySet();
    }

    public K getByValur(V v) {
        return vk.get(v);
    }

    public V getByKey(K k) {
        return kv.get(k);
    }

    public V put(K k, V v) {
        if (kv.putIfAbsent(k, v) != null) {
            throw new IllegalArgumentException("duplicate key");
        }
        if (vk.putIfAbsent(v, k) != null) {
            throw new IllegalArgumentException("duplicate value");
        }
        return v;
    }
}
